package com.linkwechat.web.controller.system;


import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.tenant.TenantContextHolder;
import com.linkwechat.common.utils.ServletUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.system.domain.SysTenant;
import com.linkwechat.system.service.ISysTenantService;
import com.linkwechat.common.utils.poi.ExcelUtil;

/**
 * 租户信息Controller
 *
 * @author ruoyi
 * @date 2021-07-05
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/tenant" )
public class SysTenantController extends BaseController {

    private final ISysTenantService iSysTenantService;


    @GetMapping("/selectTenantInfo")
    public AjaxResult selectTenantInfo(){
        // 租户集合
        List<SysTenant> sysTenants=null;
        if(TenantContextHolder.getTenantCode().equals("0")){
            sysTenants=iSysTenantService.queryAllList();
            sysTenants.add(SysTenant.builder().tenantCode("0").tenantName("平台").build());
        }else{
            sysTenants=iSysTenantService.queryList(SysTenant.builder().tenantCode(TenantContextHolder.getTenantCode()).build());
        }

        AjaxResult ajax = AjaxResult.success();
        ajax.put("sysTenants",sysTenants);
        return ajax;
    }

    /**
     * 查询租户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTenant sysTenant) {
        startPage();
        List<SysTenant> list = iSysTenantService.queryList(sysTenant);
        return getDataTable(list);
    }

    /**
     * 导出租户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:export')" )
    @Log(title = "租户信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(SysTenant sysTenant) {
        List<SysTenant> list = iSysTenantService.queryList(sysTenant);
        ExcelUtil<SysTenant> util = new ExcelUtil<SysTenant>(SysTenant.class);
        return util.exportExcel(list, "tenant" );
    }

    /**
     * 获取租户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:query')" )
    @GetMapping(value = "/{tenantCode}" )
    public AjaxResult getInfo(@PathVariable("tenantCode" ) String tenantCode) {
        return AjaxResult.success(iSysTenantService.getById(tenantCode));
    }

    /**
     * 新增租户信息
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:add')" )
    @Log(title = "租户信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTenant sysTenant) {
        return toAjax(iSysTenantService.save(sysTenant) ? 1 : 0);
    }

    /**
     * 修改租户信息
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:edit')" )
    @Log(title = "租户信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTenant sysTenant) {
        return toAjax(iSysTenantService.updateById(sysTenant) ? 1 : 0);
    }

    /**
     * 删除租户信息
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:remove')" )
    @Log(title = "租户信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{tenantCodes}" )
    public AjaxResult remove(@PathVariable String[] tenantCodes) {
        return toAjax(iSysTenantService.removeByIds(Arrays.asList(tenantCodes)) ? 1 : 0);
    }



}